<?php
namespace app\api\controller;
use app\common\controller\BaseApi;

/*好动软件人脸扩展*/
class HDApi extends  BaseApi
{
	/**
	 * 测试地址
	 * http://SmartGYM.com/api/DXApi/GetPTEmployeeList/v/1/t/1/key/zwzl_admin/sign/5de093255ad3e78341dbe00309c9436c/shop_id/68
	 * */
	 public function _initialize()
    {
    }
	/////////////////////////////////////////获取安科数据////////////////////////////////////////////////////////////////
	/**
	 * 方法前操作获取店铺信息
	 */
	function before_method(){
		if(empty(input("shop_id"))||empty(input("brand_id"))||empty(input("card_no"))){
			header("Content-type:text/html;charset=utf-8");
			echo "0";
			die;
		}
	}
	/**
	 * http://SmartGYM.com/api/HDApi/GetFaceCode/brand_id/1/shop_id/1/card_no/11111
	 */ 
	function GetFaceCode(){
		header("Content-type:text/html;charset=utf-8");
		$brand_id=input("brand_id");
		$shop_id=input("shop_id");
		$card_no=input("card_no");
		$down_key=input("down_key");
		$model=db("HdExtra")->fetchsql(FALSE)->where("brand_id='$brand_id' and card_no='$card_no'")->find();
		if(empty($model)){
			echo "-1";die;
		}else{
			if(empty($model['face_verify'])){
				echo "-1";die;
			}else{
				if($model["down_key"]==$down_key){//本地有模板
					echo "1";die;
				}else{//本地没有模板
					echo $model['down_key']."@".$model['face1'];die;
				}
			}
		}
	}
	/**
	 *  http://SmartGYM.com/api/HDApi/AjaxIndex/shop_id/75
	 * */
	function AjaxIndex(){
		$this->_ajax($this->_ajax_index());
	}
	/**
	 *  http://SmartGYM.com/api/HDApi/Delete/id/12
	 * */
	function Delete(){
		if(request()->isPost()){
			$id=input("id");
			$shop_id=input("shop_id");
			db("HdExtra")->delete($id);
			$this->api_ajax_success("","成功");	
		}else{
			$this->api_ajax_error("","非法操作");
		}
		
	}
	/**
	 *  http://SmartGYM.com/api/HDApi/Add
	 * */
	function Add(){
		if(request()->isPost()){
			$card_no=input("card_no");
			$shop_id=input("shop_id");
			$this->varifyuniq($card_no);
			$data['card_no']=$card_no;
			$data['face1']=input("face1");
			$data['icon1']=input("icon1");
			$data['shop_id']=$shop_id;
			$data['brand_id']=db("shop")->where("id=$shop_id")->value("parent_id");
			$data["face_verify"]=input("face_verify");
			$data["name"]=input("name");
			$data["mobile"]=input("mobile");
			$data["down_key"]=input("down_key");
			db("HdExtra")->insert($data);			
			$this->api_ajax_success("","成功!");	
		}else{
			$this->api_ajax_error("","非法操作!");
		}
	}
	/**
	 * http://SmartGYM.com/api/HDApi/Edit
	 * */
	function Edit(){
		if(request()->isPost()){
			$card_no=input("card_no");
			$old_card_no=input("old_card_no");
			$this->has_use($card_no,$old_card_no);
			$data["id"]=input("id");
			$data['card_no']=$card_no;
			$data['face1']=input("face1");
			$data['icon1']=input("icon1");
			$data["face_verify"]=input("face_verify");
			$data["name"]=input("name");
			$data["mobile"]=input("mobile");
			$data["down_key"]=input("down_key");
			db("HdExtra")->update($data);			
			$this->api_ajax_success("","成功!");
		}else{
			$shop_id=input("shop_id");
			$id=input("id");
			$model=db("HdExtra")->find($id);
			if(empty($model['icon1'])){
				$model["face_image1"]="http://smartgym.ocmcom.com/data/finger/a8.jpg";
			}else{
				$model["face_image1"]=$model['icon1'];
			}
			$this->_ajax($model);
		}
	} 
	//===============================================================================
	/*获取数据*/
	function _ajax_index(){
		$shop_id=input("shop_id",0);	
		$where_and=" 1=1 and brand_id=(select parent_id from m_shop where id=$shop_id)";
		if(!empty(input("card_no"))){
			$where_and.=" and card_no='".input("card_no")."'";
		}
		if(!empty(input("mobile"))){
			$where_and.=" and mobile='".input("mobile")."'";
		}
		if(!empty(input("name"))){
			$where_and.=" and name='".input("name")."'";
		}
		
		$total=db("HdExtra")->where($where_and)->fetchsql(FALSE)->count(1);
		$list=db("HdExtra")->field("id,card_no,icon1,shop_id,brand_id,face_verify,name,mobile,create_time")->where($where_and)->order("id desc")->limit(input("startIndex",0).",".input("pageSize",50))->fetchsql(FALSE)->select();
		
		return  array('pageData'=>$list,'total'=>$total,"draw"=>input("draw",0)+1);
	}
	function has_use($card_no,$old_card_no){
		$count=db("HdExtra")->where("card_no='$card_no' and card_no!='$old_card_no'")->count(1);
		 if(!empty($count)){
		 	$this->api_ajax_error("","卡号重复");
		 }
	}
	function varifyuniq($card_no){
		 $count=db("HdExtra")->where("card_no='$card_no'")->count(1);
		 if(!empty($count)){
		 	$this->api_ajax_error("","卡号重复");
		 }
	}
	  function _ajax($data=""){
    	header('Content-Type:application/json; charset=utf-8');
    	exit(json_encode($data,JSON_UNESCAPED_UNICODE));die;
    } 
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
